import * as types from "../types/editAddressTypes";
import NetWorkUtil from "../utils/NetWorkUtil";
import { isEmpty } from "../utils/utils";
import ApiConstants from "../common/ApiConstants";
import { toastShort } from "../utils/ToastUtils";
import RegexUtil from "../utils/RegexUtil";

export function newAddress(realName, mobile, area, addressDetail, setDefault, areaInfo) {
  return dispatch => {
    if (isEmpty(realName)) {
      toastShort("请输入收货人姓名");
      return;
    }
    if (isEmpty(mobile)) {
      toastShort("请输入收货人手机号");
      return;
    }
    if (!RegexUtil.checkMobile(mobile)) {
      toastShort("请输入正确的手机号");
      return;
    }
    if (isEmpty(area)) {
      toastShort("请选择地区");
      return;
    }
    if (isEmpty(addressDetail)) {
      toastShort("请输入详细地址");
      return;
    }
    dispatch({ type: types.ADDRESS_LOADING });
    const params = {
      real_name: realName,
      cell_phone: mobile,
      province_code: areaInfo.province_code,
      city_code: areaInfo.city_code,
      county_code: areaInfo.county_code,
      detail_address: addressDetail,
      default_flag: setDefault ? 1 : 0,
    };
    NetWorkUtil.post(ApiConstants.NEW_ADDRESS, params)
      .then(res => {
        toastShort("收货地址添加成功");
        dispatch({ type: types.NEWADDRESS_DONE });
      })
      .catch(error => {
        toastShort("收货地址添加失败");
        dispatch({ type: types.NEWADDRESS_ERROR, error });
      });
  };
}

export function editAddress(realName, mobile, area, addressDetail, setDefault, areaInfo, delivery_address_id) {
  return dispatch => {
    if (isEmpty(realName)) {
      toastShort("请输入收货人姓名");
      return;
    }
    if (isEmpty(mobile)) {
      toastShort("请输入收货人手机号");
      return;
    }
    if (!RegexUtil.checkMobile(mobile)) {
      toastShort("请输入正确的手机号");
      return;
    }
    if (isEmpty(area)) {
      toastShort("请选择地区");
      return;
    }
    if (isEmpty(addressDetail)) {
      toastShort("请输入详细地址");
      return;
    }
    dispatch({ type: types.ADDRESS_LOADING });
    const params = {
      real_name: realName,
      cell_phone: mobile,
      province_code: areaInfo.province_code,
      city_code: areaInfo.city_code,
      county_code: areaInfo.county_code,
      detail_address: addressDetail,
      delivery_address_id,
      default_flag: setDefault ? 1 : 0,
    };
    NetWorkUtil.post(ApiConstants.EDIT_ADDRESS, params)
      .then(res => {
        toastShort("地址保存成功");
        dispatch({ type: types.EDITADDRESS_DONE });
      })
      .catch(error => {
        toastShort("地址保存失败");
        dispatch({ type: types.EDITADDRESS_ERROR, error });
      });
  };
}

export function inputName(realName) {
  return dispatch => {
    dispatch({ type: types.ADDRESS_INPUT_NAME, realName });
  };
}

export function inputMobile(mobile) {
  return dispatch => {
    dispatch({ type: types.ADDRESS_INPUT_MOBILE, mobile });
  };
}

export function inputAddressDetail(addressDetail) {
  return dispatch => {
    dispatch({ type: types.ADDRESS_INPUT_ADDRESSDETAIL, addressDetail });
  };
}

export function chooseArea(area) {
  return dispatch => {
    dispatch({ type: types.ADDRESS_CHOOSE_AREA, area: area.join("-") });
  };
}

export function changeSetDefault(setDefault) {
  return dispatch => {
    dispatch({ type: types.ADDRESS_EDIT_SETDEFAULT, setDefault });
  };
}

export function editAddressUnmount() {
  return dispatch => {
    dispatch({ type: types.EDITADDRESS_UNMOUNT });
  };
}

export function initEdit(address) {
  return dispatch => {
    dispatch({
      type: types.EDITADDRESS_INITADDRESS,
      realName: address.real_name,
      mobile: address.cell_phone,
      area: [address.province_name, address.city_name, address.county_name].join("-"),
      addressDetail: address.detail_address,
      setDefault: address.default_flag === 1,
    });
  };
}
